import {toDouDate} from'./util'
const lunarData = require('./lunarData.js').lunarData
const monthName = require('./lunarData.js').monthName
const dayName = require('./lunarData.js').dayName
/**
 * 阳历
 */

var greDays = []
function daysNumber(count){ //给days赋值
  for (let i = 1; i <= count; i++) {
    greDays.push(toDouDate(i))
  }
}
var greMonths = []
for (let i = 1; i <= 12; i++) {
  greMonths.push(toDouDate(i))
}
//获取当前月的天数
function checkDayNumber(year, month){
  // 判断是否是闰年 闰年能被4整除且不能被100整除，或能被400整除
  var a = year % 4 == 0
  var b = year % 100 != 0
  var c = year % 400 == 0
  greDays = []
  //是否是2月
  if (month == '02') {
    if ((a && b) || c) {
      daysNumber(29);
    }else{
      daysNumber(28);
    }
  } else if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
    daysNumber(31);
  }else{
    daysNumber(30);
  }
  return greDays
}

/**
 * 阴历
 */

//获取当前年份的月份
function getLunarMonths(year) {
  var index = parseInt(year) - 1899
  var currentMon = [] //当前月的名字
  if (year == '1899'){
    return '腊'
  }
  if(year == '2020'){
    var monNum = monthName.length-1
  }else{
    var monNum = monthName.length
  }
  //判断是否闰月
  if (parseInt(lunarData[index].leapNum) != 0) {
    var lN = parseInt(lunarData[index].leapNum) - 1
    for (var i = 0; i < monNum; i++) {
      currentMon.push(monthName[i])
      if (i == lN) {
        currentMon.push('闰' + monthName[i])
      }
    }
  } else {
    for (var i = 0; i < monNum; i++) {
      currentMon.push(monthName[i])
    }
  }

  return currentMon
}

//获取当前月的天数
function getLunarDays(year,monthIndex){
  var index = parseInt(year) - 1899
  var months = lunarData[index].months
  var days = []
  //判断是否闰月
  for(var i = 0;i<months.length;i++){
    if(i == monthIndex){
      for (var j = 0; j < months[i].days;j++){
        days.push(dayName[j])
      }
    }
  }
  return days
}
//获取当前年份闰几月
function getLunarLeapNum(year){
  var index = parseInt(year) - 1899
  var lunarLeapNum = parseInt(lunarData[index].leapNum)
  if (lunarLeapNum == 0){
    return -1
  }else{
    return lunarLeapNum
  }
  
}

/**
 * 公历与农历转换
 */
function birthTypeTrans(dateType,date){
  var lunarDate = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊']
  var lunarDayDate = ['初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'] 
  var greDate = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
  var greDayDate = []
  for (var i = 1; i <= 31; i++) {
    greDayDate.push(toDouDate(i))
  }
  if (dateType == 'month'){
    
    if (isNaN(date)) {
      //输入的是农历
      if (date.length == 2) {
        var dateVal = date.substring(date.length - 1, date.length)
      } else {
        var dateVal = date
      }
      for (var i = 0; i < lunarDate.length; i++) {
        if (lunarDate[i] == dateVal) {
          return greDate[i]
        }
      }
    } else {
      //输入的是公历
      for (var i = 0; i < greDate.length; i++) {
        if (greDate[i] == date) {
          return lunarDate[i]
        }
      }
    }
  }else{
    if (isNaN(date)) {
      //输入的是农历
      for (var i = 0; i < lunarDayDate.length; i++) {
        if (lunarDayDate[i] == date) {
          return greDayDate[i]
        }
      }
    }else{
      //输入的是公历
      for (var i = 0; i < greDate.length; i++) {
        if (date == '31'){
          return '三'
        }else{
          if (greDate[i] == date) {
            return lunarDate[i]
          }
        }
        
      }
    }
  }
  
}

export  {
  checkDayNumber,//公历
  greMonths,

  getLunarMonths,//农历
  getLunarDays,
  getLunarLeapNum,

  birthTypeTrans //公历、农历转换
}